<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @property product $product
 * @property category_model $category_model
 * @property district $district
 * @property product_image $product_image
 * @property province $province
 * @property user_model $user_model
 * @property area_unit $area_unit
 * @property price_unit $price_unit
 * @property orientation $orientation
 * @property CI_DB_active_record $db
 * @property CI_DB_forge $dbforge
 * @property CI_Benchmark $benchmark
 * @property CI_Calendar $calendar
 * @property CI_Cart $cart
 * @property CI_Config $config
 * @property CI_Controller $controller
 * @property CI_Email $email
 * @property CI_Encrypt $encrypt
 * @property CI_Exceptions $exceptions
 * @property CI_Form_validation $form_validation
 * @property CI_Ftp $ftp
 * @property CI_Hooks $hooks
 * @property CI_Image_lib $image_lib
 * @property CI_Input $input
 * @property CI_Lang $lang
 * @property CI_Loader $load
 * @property CI_Log $log
 * @property CI_Model $model
 * @property CI_Output $output
 * @property CI_Pagination $pagination
 * @property CI_Parser $parser
 * @property CI_Profiler $profiler
 * @property CI_Router $router
 * @property CI_Session $session
 * @property CI_Sha1 $sha1
 * @property CI_Table $table
 * @property CI_Trackback $trackback
 * @property CI_Typography $typography
 * @property CI_Unit_test $unit_test
 * @property CI_Upload $upload
 * @property CI_URI $uri
 * @property CI_User_agent $user_agent
 * @property CI_Xmlrpc $xmlrpc
 * @property CI_Xmlrpcs $xmlrpcs
 * @property CI_Zip $zip
 * @property CI_Javascript $javascript
 * @property CI_Jquery $jquery
 * @property CI_Utf8 $utf8
 * @property CI_Security $security
 * @property slideshow_model $slideshow_model
 * @property show_style_model $show_style_model
 */

class MY_Controller extends CI_Controller
{
    public $current_authority;

    public function __construct()
    {
        parent::__construct();
		$this->load->database();
        $this->initialize_permission();
    }

    function array_to_object($array)
    {
        if(!is_array($array))
        {
            return $array;
        }

        $object = new stdClass();
        if (is_array($array) && count($array) > 0)
        {
            foreach ($array as $name=>$value) {
                $name = strtolower(trim($name));
                if (!empty($name)) {
                    $object->$name = $this->array_to_object($value);
                }
            }
            return $object;
        }
        else
        {
            return FALSE;
        }
    }

    public function send_mail($from,$to,$name,$subject,$message)
    {
        $config[]= array('protocol'=>'smtp',
            'smtp_host'=>'localhost',
            'smtp_user'=>'',
            'smtp_pass'=>'',
            'smtp_port'=>'');

        $this->email->initialize($config);

        $this->email->set_newline("\r\n");

        //From email and sender name
        $this->email->from($from,$name);

        //To addresses. if more than once, add it as comma separated
        $this->email->to($to);
        $this->email->subject($subject);
        $this->email->message($message);
        $this->email->send();
    }

	public function resize_product_image($image_path,$new_file_path,$width,$height,$is_maintain_ratio = false)
	{
		//$file_info = get_file_info($image_path);
		//if($file_info['size'] > 4 * 1024 * 1024) return false;

		$config['image_library'] = 'gd2';
		$config['source_image']	= $image_path;
		$config['new_image']	= $new_file_path;
		$config['create_thumb'] = FALSE;
		$config['thumb_marker'] = '';
		$config['maintain_ratio'] = $is_maintain_ratio;
		$config['width']	 = $width;
		$config['height']	= $height;
		$this->image_lib->initialize($config);
		$this->image_lib->resize();
	}

    public function current_user_can($permission)
    {
        return $this->permission_view_model->can($this->current_authority,$permission);
    }

    public function initialize_permission()
    {
        $user = $this->session->userdata('admin_logged');
        $this->current_authority = user_model::get_element_by_id($user)->user_group_code;
//        $user_type = $this->user_model->get_all_type_user();
//        foreach($user_type as $type)
//        {
//            $this->permission_view_model->add_authority($type->user_group_code);
//        }
        $this->permission_view_model->add_authority(user_model::$admin_type);
        $this->permission_view_model->add_authority(user_model::$diamond_type);
        $this->permission_view_model->add_authority(user_model::$livingnext_type);


        //----------------------------add view permission
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_life_dimension');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_life_dimension_category');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_life_dimension_page');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_profile');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_activity');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_connection');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_billing_name');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_billing_status');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_billing_detail');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_book_category');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_book');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_event');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_event_member');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_event_comment');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_inbox');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_notification');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_review');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_user');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_community_news');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_service_article');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_all_life_dimension_community_category');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_new_admin');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_new_diamond_staff');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_new_sos');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_all_event_type');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_new_life_dimension_category');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_all_community_type');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_all_life_dimension_type');
        $this->permission_view_model->add_permission('SUPER_ADMIN','set_service_category_default');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_all_service_category_type');
        $this->permission_view_model->add_permission('SUPER_ADMIN','add_all_dimension_category_type');

        //        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_all_life_dimension');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_all_life_dimension_category');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_all_life_dimension_page');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_diamond_profile');
        //        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_all_event');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_all_event_member');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_all_event_comment');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','add_new_diamond_staff');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','add_new_diamond_profile');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','add_diamond_event_type');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','set_service_category_default');

        //----------------------------add menu permission
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_profile_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_life_dimension_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_book_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_connection_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_community_news_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_event_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_inbox_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_measurement_unit_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_notification_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_review_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_user_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_logout_menu');
        $this->permission_view_model->add_permission('SUPER_ADMIN','view_service_menu');

        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_profile_menu');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_community_news_menu');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_event_menu');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_service_menu');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_life_dimension_menu');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_logout_menu');
        $this->permission_view_model->add_permission('DIAMOND_STAFF','view_staff_menu');
    }

}